package top.milkbox.sys.modular.permission.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.milkbox.common.annotation.CommonLog;
import top.milkbox.common.enums.LogTypeEnum;
import top.milkbox.common.validation.annotation.CollectionElementNotNull;
import top.milkbox.sys.core.config.SysConfiguration;
import top.milkbox.sys.modular.permission.param.SysPermissionAddParam;
import top.milkbox.sys.modular.permission.param.SysPermissionEditParam;
import top.milkbox.sys.modular.permission.param.SysPermissionIdParam;
import top.milkbox.sys.modular.permission.param.SysPermissionPageParam;
import top.milkbox.sys.modular.permission.service.SysPermissionService;
import top.milkbox.sys.modular.permission.vo.SysPermissionVo;
import top.milkbox.common.pojo.CommonResult;

import java.util.List;

/**
 * 权限表（sys_permission）控制器
 *
 * @author milkbox
 * @date 2024-10-31
 */
@SaCheckLogin
@RestController
@AllArgsConstructor
@RequestMapping("/sysPermission")
@Tag(name = "权限表控制器", description = "SysPermissionController")
public class SysPermissionController {

    private SysPermissionService sysPermissionService;

    @PostMapping("/add")
    @Operation(summary = "添加", description = "添加一条数据")
    @CommonLog(value = "添加", description = "添加一条数据",
            module = SysConfiguration.MODULE_NAME, type = LogTypeEnum.INSERT)
    @SaCheckPermission("/sysPermission/add")
    public CommonResult<Object> add(@Validated @RequestBody SysPermissionAddParam addParam) {
        sysPermissionService.add(addParam);
        return CommonResult.ok();
    }

    @DeleteMapping("/delete")
    @Operation(summary = "批量删除")
    @CommonLog(value = "批量删除", module = SysConfiguration.MODULE_NAME, type = LogTypeEnum.DELETE)
    @SaCheckPermission("/sysPermission/delete")
    public CommonResult<Object> delete(
            @RequestBody
            @Size(min = 1, message = "请至少传递一个删除对象")
            @CollectionElementNotNull(message = "集合中的删除对象不能为空")
            List<@Valid SysPermissionIdParam> paramList
    ) {
        sysPermissionService.delete(paramList);
        return CommonResult.ok();
    }

    @PutMapping("/edit")
    @Operation(summary = "修改", description = "修改一条数据")
    @CommonLog(value = "修改", description = "修改一条数据",
            module = SysConfiguration.MODULE_NAME, type = LogTypeEnum.UPDATE)
    @SaCheckPermission("/sysPermission/edit")
    public CommonResult<Object> edit(@Validated @RequestBody SysPermissionEditParam editParam) {
        sysPermissionService.edit(editParam);
        return CommonResult.ok();
    }

    @GetMapping("/detail")
    @Operation(summary = "详情", description = "查询一条数据的详情")
    @CommonLog(value = "详情", description = "查询一条数据的详情",
            module = SysConfiguration.MODULE_NAME, type = LogTypeEnum.SELECT)
    @SaCheckPermission("/sysPermission/detail")
    public CommonResult<SysPermissionVo> detail(@Validated SysPermissionIdParam idParam) {
        return CommonResult.ok(sysPermissionService.detail(idParam));
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询", description = "查询条件分页查询")
    @CommonLog(value = "分页查询", description = "查询条件分页查询",
            module = SysConfiguration.MODULE_NAME, type = LogTypeEnum.SELECT)
    @SaCheckPermission("/sysPermission/page")
    public CommonResult<Page<SysPermissionVo>> page(@Validated SysPermissionPageParam pageParam) {
        return CommonResult.ok(sysPermissionService.page(pageParam));
    }

}